
import Component from 'vue-class-component';
import Vue from 'vue';
import Header from '@/utils/Header';
import help from './help.md';
import axios from 'axios';
import Ajax from '@/utils/Ajax';
import ShanbayUtils from './ShanbayUtils';
import IShanbayWord from './IShanbayWord';
@Component({

})
export default class ShanbayImportFromYoudao extends Vue {
    private words: IShanbayWord[] = [];
    private shanbayToken = '';
    private setting;
    private created() {

        this.loadSetting();

        ShanbayUtils.getLocal(this.words);

        Header.setHeader({
            title: '扇贝单词从有道导入',
            desp: '',
            help,
        });

        this.shanbayToken = ShanbayUtils.readToken();
        window.shanbayLogin = (params) => {
            this.shanbayToken = ShanbayUtils.getToken(params);
            ShanbayUtils.saveToken(this.shanbayToken);
            if (this.shanbayToken) {
                this.submitWords();
            }
        };
    }
    /**
     * 加载配置
     */
    private async loadSetting() {
        this.setting = (await Ajax.get('/api/shanbay/getsetting')).data;
    }
    private async onChooseFile(event) {
        await ShanbayUtils.decodeFile(event.target.files, this.words);
        ShanbayUtils.setLocal(this.words);
    }
    private onDel(index) {
        this.words.splice(index, 1);
    }
    private onGetFirstTen() {
        const ten = this.words.splice(0, 10);
        let str = '';
        for (const word of ten) {
            str += word.word + '\n';
        }
        if (str) {
            window.prompt('复制到剪切板: Ctrl+C, Enter', str);
        }
        ShanbayUtils.setLocal(this.words);
    }
    private onClear() {
        this.words = [];
        ShanbayUtils.setLocal(this.words);
    }
    private async submitWords() {
        for (let i = 0; i < this.words.length; i++) {
            const word = this.words[i];
            word.msg = '提交中';
            const ret = await Ajax.post('/api/shanbay/addword', {
                word: word.word,
                token: this.shanbayToken,
            });
            if (ret.success) {
                this.words.splice(i, 1);
                i--;
                ShanbayUtils.setLocal(this.words);
                continue;
            }
            if (ret.err_code === 1) {
                ShanbayUtils.openTokenWin(this.setting.appkey);
                return;
            }
            if (ret.msg.indexOf('405') >= 0) {
                word.msg = '访问次数过多,请1小时后再来或手动导入';
                window.alert('导入次数过多!!请1小时后再来或手动导入');
                return;
            } else {
                this.setMsg(word, ret.msg);
            }
        }
    }
    private async onSubmit() {
        if (!this.setting) {
            alert('获取配置错误，请刷新后重试。假如重复重现，请提issue');
            return;
        }
        if (!this.shanbayToken) {
            ShanbayUtils.openTokenWin(this.setting.appkey);
            return;
        } else {
            this.submitWords();
        }
    }
    private setMsg(word: IShanbayWord, msg) {
        Vue.set(word, 'msg', msg);
    }
}
